<template>
  <div class="full" style="background: #fff">
    <div class="weui-msg" v-show="!paySuc">
      <div class="weui-msg__icon-area"><i class="weui-icon-success weui-icon_msg"></i></div>
      <div class="weui-msg__text-area">
          <h2 class="weui-msg__title">信息录入成功</h2>
          <p class="weui-msg__desc" v-text="itemInfoTip"></p>
          <!-- <p class="weui-msg__desc">购买200钻石,即赠送二级代理权限</p> -->
          <!-- <p class="weui-msg__desc">购买500钻石,即赠送一级代理权限</p> -->
      </div>
      <div class="weui-msg__opr-area">
        <p class="weui-btn-area">
          <a @click="confirm" class="weui-btn weui-btn_primary">立即购买</a>
          <!-- <a @click="confirmFirst" class="weui-btn weui-btn_primary">购买500钻石</a> -->
          <!-- <a @click="confirmSecond" class="weui-btn weui-btn_primary">购买200钻石</a> -->
          <a @click="back" class="weui-btn weui-btn_default">返回</a>
        </p>
      </div>
      <div class="weui-msg__extra-area">
        <div class="weui-footer">
          <p class="weui-footer__text">乐翻天@2017</p>
        </div>
      </div>
    </div>
    <div class="weui-msg" v-show="paySuc">
      <div class="weui-msg__icon-area"><i class="weui-icon-success weui-icon_msg"></i></div>
      <div class="weui-msg__text-area">
          <h2 class="weui-msg__title">代理申请成功!</h2> 
          <p class="weui-msg__desc"></p>  
      </div>
      <div class="weui-msg__extra-area">
        <div class="weui-footer">
          <p class="weui-footer__text">乐翻天@2017</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: ['inviteId', 'shoppingItem', 'proxyLevel'],
  data() {
    const token = this.getQueryString('token')
    const areaCode = this.getQueryString('areaCode')
    const config = {
      agentSystem: 'https://c-c-a.jiahuagame.com/game-lobby/',
    }
    if (window.location.hostname.match(/10.10.10/)) {
      // config.pre = 'https://10.10.10.107:8080/game-lobby'
      config.agentSystem = 'https://lefantian.jiahuagame.com/game-lobby'
    } else if (window.location.hostname.match(/lefantian/)) {
      config.agentSystem = 'https://lefantian.jiahuagame.com/game-lobby'
    } else {
      config.agentSystem = 'https://c-c-a.jiahuagame.com/game-lobby'
    }
    const api = {
      // URL：{path}/Order/shopping/{areaCode}/{goods_type}/{matket_type}
      getShoppingItems(level) {
        const url = `${config.agentSystem}/Order/shopping/${areaCode}/1/${level}`;
        const opt = {
          method: 'GET',
          headers: {
            Authorization: token,
            'Content-type': 'application/json'
          }
        }
        return fetch(url, opt)
      },
      // URL：{path}/agent/apply/{areaCode}/{orderNo}/audit
      audit(orderNo) {
        const url = `${config.agentSystem}/agent/apply/${areaCode}/${orderNo}/audit`;
        const opt = {
          method: 'GET',
          headers: {
            Authorization: token,
            'Content-type': 'application/json'
          }
        }
        return fetch(url, opt)
      },
      generateOrder(params, lftid) {
        const url = `${config.agentSystem}/Order/${areaCode}/${lftid}`
        const opt = {
          method: 'POST',
          body: JSON.stringify(params),
          headers: {
            Authorization: token,
            'Content-type': 'application/json'
          }
        }
        console.log(opt)
        return fetch(url, opt)
      },
    }
    return {
      api,
      paySuc: false
    }
  },
  mounted() {
    // this.api.getShoppingItems(1).then(res => res.json()).then(data => {
    //   console.log('getShoppingItems', data)
    //   if (data.result) {
    //     $.alert('获取购买信息失败');
    //     return 
    //   }
    //   this.shoppingFirstItem = data.data[0]
    // })
    // this.api.getShoppingItems(2).then(res => res.json()).then(data => {
    //   console.log('getShoppingItems', data)
    //   if (data.result) {
    //     $.alert('获取购买信息失败');
    //     return 
    //   }
    //   this.shoppingSecondItem = data.data[0]
    // })
  },
  methods: {
    getQueryString(name) {
      var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
      var r = window.location.search.substr(1).match(reg);
      if (r != null) {
        return unescape(r[2]);
      }
      return null;
    },
    confirm() {
      if (!this.shoppingItem) {
        return 
      }
      const params = [{
        gid: this.shoppingItem.gid,
        quantity: 1
      }]
      this.api.generateOrder(params, this.inviteId).then(res => res.json()).then(data => {
        console.log(data);
        if (data.result) {
          $.alert('生成订单失败')
          return
        }
        this.onBridgeReady(data.data)
      })
    },
    onBridgeReady(data) {
      // const { timestamp, nonceStr, package, signType, paySign, appId } = data
      const onComplete = res => {
        if (res.err_msg === 'get_brand_wcpay_request:ok') {
          // this.paySuc = true
          // alert(JSON.stringify(data))
          this.api.audit(data.orderNo).then(res => res.json()).then(data => {
            if (data.result) {
              $.alert(data.message)
              return
            }
            this.paySuc = true
          })
        }
      }
      WeixinJSBridge.invoke('getBrandWCPayRequest', data, onComplete)
    },
    back () {
      this.$emit('back')
    }
  },
  computed: {
    itemInfoTip() {
      if (!this.shoppingItem) {
        return ''
      }
      const proxyLevel = this.proxyLevel === 2 ? '一级代理' : '二级代理'
      return `购买${this.shoppingItem.price}钻石,即赠送${proxyLevel}权限` 
    }
  },
  components: {
  }
}

</script>

<style lang="less">
</style>